﻿using S2_1.Models;
using S2_1.Repositories;
using S2_1.Services.Models;
using System;
using System.Collections.Generic;

namespace S2_1.Services
{
    public partial class UserService : IDisposable
    {
        private readonly UserRepository _productRepository;
        public UserService()
        {
            _productRepository = new UserRepository();
        }


        /// <summary>
        /// 获取用户个人资料
        /// </summary>
        public AppResult<ProfileOutput> GetProfile(string email)
        {
            var user = _productRepository.Single(email);
            if (user == null) return new AppResult<ProfileOutput>(false, "没有找到个人资料");

            var profileOutput = new ProfileOutput()
            {
                Emial = user.Emial,
                FirstName = user.FirstName,
                LastName = user.LastName,
                Phone = user.Phone,
                Picture = user.Picture,
                配送方式 = user.配送方式
            };
            return new AppResult<ProfileOutput>(profileOutput);
        }

        /// <summary>
        /// 通过邮箱和密码获取用户
        /// </summary>
        /// <param name="emial"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public User GetUserByEmailAndPassword(string emial, string password)
        {
            password = MD5Encryption.Encrypt(password);
            return _productRepository.Single(emial, password);
        }

        /// <summary>
        /// 获取安全问题
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>

        public string GetSafeQuestion(string email)
        {
            var user = _productRepository.Single(email);
            return user.安全问题;
        }
        public AppResult RestPassword(ResetPasswordInput resetPasswordInput)
        {
            var result = new AppResult();
            var user = _productRepository.Single(resetPasswordInput.Email);
            if (user == null)
            {
                return new AppResult("没有找到用户");
            }
            else if (resetPasswordInput.Answer != user.安全答案)
            {
                return new AppResult(message: "安全问题答案错误");
            }
            var password = MD5Encryption.Encrypt(resetPasswordInput.NewPassword);
            _productRepository.UpdatePassword(user.Emial, password);
            return result;
        }

        /// <summary>
        /// 获取所有产品
        /// </summary>
        /// <returns></returns>
        public IEnumerable<User> GetAll()
        {
            return _productRepository.All();
        }


        /// <summary>
        /// 注册新用户
        /// </summary>
        /// <param name="product">新产品对象</param>
        /// <returns></returns>
        public bool Add(User user)
        {
            user.Password = MD5Encryption.Encrypt(user.Password);
            user.安全问题 = MD5Encryption.Encrypt(user.安全问题);
            return _productRepository.Add(user);
        }


        /// <summary>
        /// 根据ID，删除一个产品
        /// </summary>
        /// <param name="id">产品ID</param>
        /// <returns></returns>
        public bool Remove(int id)
        {
            return _productRepository.RemoveSingle(id);
        }
        public void Dispose()
        {
            _productRepository.Dispose();
        }
    }
}